<template>
	<view>
		<view class="empty" v-if="show==false">
			<view class="empty-text">空空如也的购物</view>
			<view class="empty-button" @click="goshopping">去选购</view>
		</view>
		<view class="show" v-if="show==true">
			<view class="goods-detail" v-for="(item,index) in cartList" :key="index"
				@touchstart="itemTouchStart($event, index)" @touchmove="itemTouchMove($event, index)"
				@touchend="itemTouchEnd($event, index)" @click="oneself"
				:style="{ transform: activeIndex === index ? 'translateX( ' + itemTransLateX + 'px)' : 0, transition: 'all 0.4s' }">
				<div class="detail">
					<view class="detail-left">
						<view class="goods-left">
							<checkbox-group @change="selected(item)"><label>
									<checkbox class="selected" color="#555555" :checked="item.flag" /><text></text>
								</label></checkbox-group>
							<image :src="item.timesUrl" style="width: 150rpx;height: 140rpx;"></image>
						</view>
						<view class="size" @tap="productDetail(item)">
							<text style="font-size: 25rpx;"
								class="product-name">{{item.timeText3}}撒上看到几个啥都不能看你的手机壳鲨大富豪科级干部可多个发阿萨德开工后付款计划</text>
							<text class="goods-price">￥{{item.price}}/件</text>
						</view>
					</view>
					<view class="detail-right">
						<text class="subtract" @click="reduce(item)">-</text>
						<text class="num">{{item.num}}</text><text @click="add(item)" class="add">+</text>
					</view>
				</div>
				<view class="del" @click.stop="delCarSingle(index)" @touchend.stop>
					<text>删除</text>
				</view>
			</view>
		</view>
		<view class="end">
			<view class="end-left"><checkbox-group @change="selectgoods()"><label>
						<checkbox :checked="allchecked" />全选
					</label></checkbox-group>
				<view>总计：<text style="color: #f00;font-weight: bold;">￥ {{totalPrice.toFixed(2)}}</text></view>
			</view>
			<view class="end-right" @tap="fun3">结算({{totalNum}})</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				show: true,
				// show: false,
				allchecked: true,
				checked: true,
				cartList: [], //购物车商品
				totalPrice: 0,
				totalNum: 0,
				activeIndex: 0,
				startX: 0,
				itemTransLateX: 0,
			}
		},
		onLoad(e) {
			//初始化购物车
			const obj = uni.getStorageSync('cart_list');
			if (obj) {
				let cartList = JSON.parse(obj);
				cartList.forEach((item, index) => {
					item['flag'] = true;
				})
				this.cartList = cartList;
				this.total();
			}
			if (this.cartList == []) {
				this.show = false
			} else {
				this.show = true
			}
		},

		methods: {
			oneself() {

			},
			// 手指触摸事件
			itemTouchStart(e, index) {
				this.activeIndex = index;
				this.startX = e.changedTouches[0].pageX;
				// console.log('手指触摸时', this.startX);
			},
			// 手指移动事件
			itemTouchMove(e) {
				let moveX = e.changedTouches[0].pageX;
				this.itemTransLateX = moveX - this.startX >= 0 ? 0 : moveX - this.startX;
				// console.log('手指移动了', this.itemTransLateX);
			},
			// 手指离开事件
			itemTouchEnd(e, index) {
				let endX = e.changedTouches[0].pageX;
				// 如果手指离开和手指触摸时的值一致则是点击事件的行为，不以滑动的逻辑处理此行为
				if (endX == this.startX) {
					if (this.itemTransLateX !== 0) return (this.itemTransLateX = 0);
					return;
				}
				this.itemTransLateX = endX - this.startX;
				// console.log('移动量', this.itemTransLateX);
				this.itemTransLateX = this.itemTransLateX <= -41 ? -82 : 0;
				// console.log('手指离开时', endX);
			},
			delCarSingle(index) { //删除购物车商品 - 单个
				const obj = uni.getStorageSync('cart_list');
				if (obj) {
					this.cartList = JSON.parse(obj);
				}
				if (this.cartList.length > index) {
					//删除
					this.cartList.splice(index, 1);
					this.itemTransLateX=0
				}

				try {
					//存到缓存
					this.saveCarToStorage();
				} catch (e) {
					uni.showToast({
						title: '删除失败',
						icon: 'error',
					});
				}
			},
			saveCarToStorage() { //缓存购物车
				uni.setStorageSync('cart_list', JSON.stringify(this.cartList));
				this.total();
				this.show = true
				this.showw = false
			},
			fun3() {
				uni.navigateTo({
					url: "./flowwer"
				})
				setTimeout(() => {
					uni.$emit('pink', this.cartList)
				}, 300)
			},
			goshopping() {
				uni.navigateTo({
					url: '../about/about'
				})
			},
			change(e) {
				console.log(e)
			},
			selected(item) {
				item.flag = !item.flag
				if (!item.flag) {
					this.allchecked = false
				} else {
					let allchecked = true;
					this.cartList.forEach((item) => {
						if (!item.flag) {
							allchecked = false;
						}
					})
					this.allchecked = allchecked
				}
				this.total();
			},
			selectgoods() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.cartList.map((item) => {
						this.checked = true
						item.flag = true
					})
				} else {
					this.checked = false
					this.cartList.map((item) => {
						item.flag = false
					})
				}
				this.total();
			},
			add(item) {
				let num = item.num;
				item.num = num + 1;
				//存到缓存
				this.saveCarToStorage();
				this.total();
			},
			reduce(item) {
				let num = item.num
				if (num > 1) {
					num -= 1
				} else if ((num = 1)) {
					uni.showToast({
						title: '该宝贝不能减少了哟~'
					})
				}
				item.num = num;
				//存到缓存
				this.saveCarToStorage();
				this.total();
			},
			total() {
				this.totalNum = 0;
				this.totalPrice = 0
				this.cartList.forEach((item) => {
					item.flag ? (this.totalNum += item.num) : (this.totalNum += 0)
					item.flag ? (this.totalPrice += item.num * item.price) : (this.totalPrice += 0);
				})
			},
			productDetail(item) { //商品详情
				uni.navigateTo({
					url: `../detail/detail?arr=${JSON.stringify(item)}`
				});
			},
		}
	}
</script>
<style lang="scss">
	.show {
		width: 100%;
	}

	.goods {

		line-height: 80rpx;
		background-color: #ffffff;

		&-detail {
			display: flex;
			width: 118vw;
			padding: 30rpx 15rpx 30rpx 30rpx;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 5rpx solid #f1f1f1;
			align-items: center;

			.detail {
				width: 100%;
				display: flex;
				justify-content: space-between;
				overflow: hidden;
			}

			.del {
				width: 100px;
				height: 100px;
				background-color: #f47261;
				color: white;
				text-align: center;
				line-height: 100px;
			}

			.detail-left {
				flex: 1;
				display: flex;
				overflow: hidden;

				.goods-left {
					display: flex;
					align-items: center;
				}
			}

			.size {
				display: flex;
				justify-content: space-around;
				flex-direction: column;
				margin-left: 30rpx;

				.goods-price {
					font-size: 25rpx;
					color: #f44545;
				}

				.product-name {
					overflow: hidden; //溢出内容隐藏
					text-overflow: ellipsis; //文本溢出部分用省略号表示
					display: -webkit-box; //特别显示模式
					-webkit-line-clamp: 2; //行数
					line-clamp: 2;
					-webkit-box-orient: vertical; //盒子中内容竖直排列
				}
			}

			.detail-right {
				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #f7f7f7;
					margin-right: 10rpx;
				}

				.add {
					color: #fa4305;
					border-radius: 10rpx 30rpx 30rpx 10rpx;
					margin-right: 20rpx;
				}

				.subtract {
					border-radius: 30rpx 10rpx 10rpx 30rpx;
				}
			}
		}
	}

	.empty {
		position: relative;
		top: 220rpx;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;

		&-text {
			color: #808080;
			margin-bottom: 50rpx;
		}

		&-button {
			width: 300rpx;
			height: 90rpx;
			color: orange;
			border: 1rpx solid orange;
			text-align: center;
			line-height: 90rpx;
			border-radius: 48rpx;
		}
	}

	.end {
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		position: fixed;
		bottom: 100rpx;
		left: 0;
		display: flex;
		align-items: center;

		&-left {
			width: 70%;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;

			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			width: 30%;
			line-height: 90rpx;
			background-color: #f44545;
			text-align: center;
			color: #fff;
		}
	}
</style>